<template>
	<view>
		<u-cell-group v-for="(val, key,index) in urls" :key="index" :title="'附件：'+key" :border="true">
			<u-album :urls="val" :multipleSize="70"></u-album>
		</u-cell-group>
		<u-cell-group :border="true" title="附件:">
			<u-cell v-for="(file,index) in files" :key="index" :title="file.Name">
					<view slot="right-icon" style="display: flex; width: 60px; justify-content: space-around;">
						<u-icon name="eye" @click="toView(file.Id)" v-if="canView(file.Name)"></u-icon>
						<u-icon name="arrow-downward"  @click="download(file)" ></u-icon>
					</view>
			</u-cell>
		</u-cell-group>
	</view>
</template>

<script>
	import {
		getfilebyoutid
	} from '@/common/api.js'
	import {
		baseUrl
	} from '@/common/config.js'
	export default {
		props: {
			outid: {}
		},
		data() {
			return {
				urls: [],
				files: [],
				pdfSrc: "",
				modalShow: false,
				mydata: {
					Name: '',
					Dept: {
						Name: ''
					}
				}
			}
		},
		watch: {

			outid: {

				handler(newv, oldv) {
					this.getdate()

				}

			}

		},
		onLoad(option) {
			// this.buildid = option.buildid
			// if (option.buildid)

		},
		mounted() {
			this.getdate()
		},

		methods: {
			showmap() {
				uni.navigateTo({
					url: 'map?buildid=' + this.buildid
				})
			},
			getFileExtension(filename) {
				return filename.split(".").pop().toLowerCase()
			},

			getdate() {
				// this.mydata={Name:'22',Addr:'',ProjInfo:{Name:''},BuildManager:""}
				getfilebyoutid(this.outid).then(res => {
					// this.mydata={Name:'33',Addr:'',ProjInfo:{Name:''},BuildManager:""}
					// console.log(this.data)
					var iurls = {}
					var ifiles = []
					res.forEach(e => {
						var ext = this.getFileExtension(e.Name)
						if (ext == "jpg" || ext == "png" || ext == "bmp" || ext == "gif") {
							if (!iurls[e.MyType]) {
								iurls[e.MyType] = []
							}
							iurls[e.MyType].push(`${baseUrl}/SysFile/download?id=${e.Id}`)
							// iurls.push(`${baseUrl}/SysFile/download?id=${e.Id}`)
						} else
							ifiles.push(e)
					})

					this.urls = iurls
					this.files = ifiles
					// this.mydata = res.data[0];
					// console.log(this.data)
				})
			},
			download(file) {
				var url = `${baseUrl}/SysFile/download?id=${file.Id}`
				uni.downloadFile({
					url: url, //下载地址接口返回
					filePath: wx.env.USER_DATA_PATH + '/' + file.Name,
					success: (data) => {
								console.log(data.filePath)
								console.log(JSON.stringify(data))
								if (data.statusCode === 200) {
									// uni.saveFile({ //文件保存到本地
									// 	tempFilePath: data.filePath, //临时路径
									// 	success: function(res) {
									// 		console.log("下载成功"+res.savedFilePath)
									// 		console.log(JSON.stringify(res))
									// 		uni.showToast({
									// 			icon: 'none',
									// 			mask: true,
									// 			title: '文件已保存!', 
									// 			duration: 3000,
									// 		});
											
									// 	}
									// });
									uni.openDocument({
										//fileType: 'docx',
										showMenu:true, //关键点,可以转发到微信
										filePath: data.filePath,
										success: function(res) {
											console.log('打开文档成功');
										}
									});
								}
							},
					fail: (err) => {
						console.log(err);
						uni.showToast({
							icon: 'none',
							mask: true,
							title: '失败请重新下载',
						});
					},
				});
			},
			toView(id){
				this.pdfSrc = "https://project.gxrunxiang.com/SysFile/view?id=" + id;
				uni.previewImage({
				    urls: [this.pdfSrc]
				})
			},
			canView(v){
			        let list = v.split('.')
			        if(list.length==0)
			        return false
			        let ext = list[list.length-1].toLowerCase()
			        if("xls,xlsx,docx,doc,pdf".split(',').indexOf(ext)!=-1)
			        {
			          return true
			        }
			        return false
			 },
		}
	}
</script>

<style>

</style>